var news = liwe.module ( "news", { "news_show": "_show", "news_categ_search": "_categ_search", "news_list" : "_news_list" } );

news.templates = null;

news.cbacks = {
	'show': null,
	'before_show': null,	// func ( data )
	'search': null,
	'before_search': null,
	'row_manip': null
};

news.cnts_navi = [];

news.init = function ( cback )
{
	news.valid_events ( [ "show", "render", "before-show", "search", "before-search", "row-manip", "show-results" ] );

	news.cnts_navi = [];

	news.load_templates ( cback );

	news.addEventListener ( "render", function ( evt ) 
	{
		var p = evt.params;
		var n = p [ 'news' ];	
		$( p.dest, n [ '_html' ] );
	}, true );

};

news.list = function ( dct, cback )
{
	if ( ! dct ) dct = {};

	dct [ 'action' ] = "news.ajax.list";

	liwe.AJAX.easy ( dct, function ( v )
		{
			cback && cback ( v );
		} );
};

news.get = function ( id_news, permalink, cback )
{
	liwe.AJAX.easy ( { action: "news.ajax.get", 'id_news': id_news, 'permalink' : permalink }, function ( v ) {
		cback && cback ( v );
	} );
};

news._show = function ( data )
{
	if ( news.cbacks.get ( 'before_show' ) ) 
	{
		console.warn ( "DEPRECATION WARNING: news cbacks [before_show]" );
		news.cbacks.get ( 'before_show' ) ( data );
	}

	news.dispatchEvent ( "before-show", null, data );

	news.show ( data [ 'id_news' ], data [ 'dest' ], data [ 'permalink' ] );
};

news.show = function ( id_news, dest, permalink, pos )
{
	news.set_history ( "news_show", news._show, { "id_news" : id_news, "dest" : dest, "permalink" : permalink } );

	news.get ( id_news, permalink, function ( v ) {
		var n = v.get ( 'news' );
		var html = '';

		if ( n )
		{
			n [ 'template' ] = n [ '_html' ];
			n [ '_html' ] = String.formatDict ( n.get ( '_html', '' ), n );
			if ( typeof ( social ) !== 'undefined' )
			{
				n [ '_fb_like' ] = String.formatDict ( news.templates [ 'FACEBOOK_LIKE' ], n );
				n [ '_fb_comment' ] = String.formatDict ( news.templates [ 'FACEBOOK_COMMENT' ], n );
			};
		
		};
		
		// FIXME: event_fired dovrebbe essere il valore tornato da news.dispatchEvent ()
		// 	  inoltre, ricordarsi che cbacks [ 'show' ] mandava piu' parametri, mentre
		// 	  l'evento manda un dizionario
		var event_fired = 0;

		if ( news.cbacks.get ( 'show' ) ) 
		{
			console.warn ( "DEPRECATION WARNING: news cbacks [show]" );
			news.cbacks [ 'show' ] ( n, id_news, dest, permalink, pos );

			event_fired = 1;
		} 

		if ( news.dispatchEvent ( "show", null, { "news" : n, "id_news" : id_news, "dest" : dest, "permalink" : permalink, "pos" : pos } ) ) event_fired = 1;
		if ( ! event_fired )
		{
			news.dispatchEvent ( "render", null, { "news" : n, "id_news" : id_news, "dest" : dest, "permalink" : permalink, "pos" : pos } );
			news._add_fb_content ( id_news );
			news.navi_render ( id_news, dest, permalink, pos );
		}
	} );
};


news.navi_render = function ( id_news, dest, permalink, pos )
{
	var t, l = news.cnts_navi.length
	if ( l <= 0 ) return;

	var in_navi = ( news.ds && pos != null );

	var num_rows = news.ds ? news.ds.num_rows : 0;
	var prev = null;
	var next = null;

	var id_prev_row = null;
	var id_next_row = null;

	if ( in_navi )
	{
		pos = parseInt ( pos, 10 );

		prev = (pos > 0) ? (pos - 1) : null;
		next = (pos < num_rows - 1) ? (pos + 1) : null;

		news.ds.prefetch ( ( prev ? prev : 0 ), function ()
		{
			news.ds.prefetch ( ( next ? next : 0 ), function ()
			{
				if ( prev != null ) id_prev_row = news.ds.get_row ( prev ) [ "id" ];
				if ( next != null ) id_next_row = news.ds.get_row ( next ) [ "id" ];

				var s = '';
				if ( id_prev_row ) s += String.formatDict ( news.templates [ 'NEWS_SHOW_NAVI_PREV' ], { id: id_prev_row, dest: dest, permalink: permalink, pos: prev } );
				if ( id_next_row ) s += String.formatDict ( news.templates [ 'NEWS_SHOW_NAVI_NEXT' ], { id: id_next_row, dest: dest, permalink: permalink, pos: next } );

				for ( t = 0; t < l; t ++ )
				{
					var cnt_nav = news.cnts_navi [ t ];
					if ( ! $ ( cnt_nav ) ) continue;

					$ ( cnt_nav ).innerHTML = s;
				}
			} );
		} );
	}
};

news.categ_search = function ( id_categ, dest, name, lines )
{
	news.set_history ( "news_categ_search", null, { 'id_categ': id_categ, dest: dest, lbl_categ: name, 'lines': lines } );

	var dct = {};
	dct [ 'id_categs' ] = id_categ;

	news.init_ds_search ( 'Correlati: ' + name, lines );

	news._search_start ( dct, dest );
};

news._check_templates = function ( cback )
{
	if ( ! news [ 'templates' ] ) setTimeout ( function () { news._check_templates ( cback ); }, 300 );
	else cback ();
};

news._categ_search = function ( data )
{
	news._check_templates ( function () {
		news.categ_search ( data [ 'id_categ' ], data [ 'dest' ], data [ 'lbl_categ' ], data [ 'lines' ] );
	} );
};


news.search = function ( txt, dest, name, lines )
{
	if ( !txt ) return;

	news.set_history ( "news_search", function ( data ) 
		{
			news.search ( data [ 'txt' ], data [ 'dest' ] ); 
		},
		{ 'testo': txt, dest: dest } 
	);

	var dct = {};
	dct [ 'testo' ] = txt;

	news.init_ds_search ( 'Correlati: ' + name, lines );

	news._search_start ( dct, dest );
};

news.tag_search = function ( tag, dest, lines, exclude_tags )
{
	news.set_history ( "news_tag_search", function ( data ) {
		news.tag_search ( data [ 'tag' ], data [ 'dest' ], data [ 'lines' ], data [ 'exclude_tags' ] ); 
		}, { tag: tag, dest: dest, lines: lines, exclude_tags: exclude_tags } 
	);

	var dct = {};

	dct [ 'tags' ] = tag;
	dct [ 'exclude_tags' ] = exclude_tags;

	//news.list ( dct, function ( v ) {
		//
	//} );

	news.init_ds_search ( 'Correlati: ' + tag, lines );
	//news.ds.lines_per_page = 1;

	
	news._search_start ( dct, dest );
};

news._news_list = function ( data )
{

	news.list_ds ( data [ 'mode' ], data [ 'dest' ], data [ 'lines' ] );
};

news.list_ds = function ( mode, dest, lines )
{
	news.set_history ( "news_list", function ( data ) {
		news.list_ds ( data [ 'mode' ], data [ 'dest' ], data [ 'lines' ] );
		}, {  mode: mode, dest: dest, lines: lines }
	);

	var dct = {};

	dct [ 'action' ] = "news.ajax.list";

	//news.list ( dct, function ( v ) {
		//
	//} );

	news.init_ds_search ( 'Correlati: ' + mode, lines );
	//news.ds.lines_per_page = 1;

	
	news._search_start ( dct, dest );
};

news.init_ds_search = function ( titolo, lines )
{
	news.ds = new DataSet ( "ds_news", "/ajax.pyhp" );
	if ( ! titolo ) titolo = 'News';

	news._titolo = titolo;

	news.ds.templates [ 'table_start' ] = String.formatDict ( news.templates [ 'ds_news_start' ], { 'titolo_box': titolo } );
	news.ds.templates [ 'table_end' ]   = news.templates [ 'ds_news_end' ];
	news.ds.templates [ 'table_header' ] = news.templates [ 'ds_news_header' ];
	news.ds.templates [ 'table_footer' ] = news.templates [ 'ds_news_footer' ];
	
	news.ds.templates [ 'table_row' ] = news.templates [ 'ds_news_row' ];
	news.ds.templates [ 'prev_page' ] = news.templates [ 'ds_prev_page' ];
	news.ds.templates [ 'next_page' ] = news.templates [ 'ds_next_page' ];

	news.ds.paginator.templates [ "pag-link-space" ] = "&nbsp;&nbsp;";
	news.ds.paginator.templates [ "pag-sep" ] = "-";
	news.ds.paginator.templates [ "pag-first" ] = "&lt;&lt;";
	news.ds.paginator.templates [ "pag-last" ] = "&gt;&gt;";
	news.ds.paginator.templates [ "pag-right-info" ] = ' | <span class="pagi_res">Pag. <b>%(_PAGE)s</b> di <b>%(_TOT_PAGES)s</b></span>';

	news.ds.lines_per_page = lines ? lines : 10;

	news.ds.cbacks [ 'row_manip' ] = news._row_manip;
	news.ds.cbacks [ 'fill_end' ] = news._fill_end;

	news.ds.cbacks [ 'show_results' ] = news._show_results_cb;

};

news._show_results_cb = function ( ds )
{
	if ( news.cbacks.get ( 'show_results' ) ) 
	{
		console.warn ( "DEPRECATION WARNING: news cbacks [show_results]" );
		news.cbacks [ 'show_results' ] ( ds );
	}
	news.dispatchEvent ( "show-results", null, { "ds" : ds } );
};

news._search_start = function ( dict, dest )
{
	if ( ! dict ) dict = {};

	if ( ! dest ) dest = "block_main";

	news._dest = dest;

	dict [ 'action' ] = "news.ajax.search";

	if ( news.cbacks.get ( 'search' ) )
	{
		console.warn ( "DEPRECATION WARNING: news cbacks [search]" );
		news.cbacks [ 'search' ] ();
	}

	news.dispatchEvent ( 'search', null, null );

	news.ds.set_fields ( dict );
	
	news.ds.fill ( news._show_results );
};


news._show_results = function  ()
{
	if ( news.cbacks.get ( 'before_search' ) )
	{
		console.warn ( "DEPRECATION WARNING: news cbacks [before_search]" );
		news.cbacks [ 'before_search' ] ();
	}

	news.dispatchEvent ( 'before-search', null, null );

	if ( news.ds.num_rows <= 0 ) $ ( news._dest ).innerHTML = String.formatDict ( news.templates [ 'no_result' ], { titolo_box: news._titolo } );
	else news.ds.render ( news._dest );
};

news._row_manip = function ( ds, row )
{
	if ( row [ '_img' ] == '-1' )
		row [ '_img' ] = news.templates [ 'NO_FOTO' ];

	row [ 'dest' ] = news._dest;
	
	row [ '_fb_like' ] = '';
	row [ '_fb_comment' ] = '';

	if ( typeof ( social ) !== 'undefined' )
	{
		row [ '_fb_like' ] = String.formatDict ( news.templates [ 'FACEBOOK_LIKE' ], row );
		row [ '_fb_comment' ] = String.formatDict ( news.templates [ 'FACEBOOK_COMMENT' ], row );
	};

	if ( news.cbacks.get ( 'row_manip' ) )
	{
		console.warn ( "DEPRECATION WARNING: news cbacks [row_manip]" );
		news.cbacks [ 'row_manip' ] ( ds, row );
	}

	news.dispatchEvent ( 'row-manip', null, { "ds" : ds, "row" : row } );
};

news._fill_end = function ( ds, vars )
{
	if ( typeof ( social ) == 'undefined' ) return;
	var ids = [], row;
	for ( i = 0; i< vars [ 'lines' ]; i++ )
	{
		row = vars [ 'row' + i ] ;
		if ( ! row ) continue;
		if ( ! row.get ( "id" ) ) continue;

		//get all id
		ids.push ( row [ 'id' ].toString() ) ;
	};
	news._add_fb_content ( ids.join ( '|' ) );
};

news._add_fb_content = function ( ids )
{
	//add facebook like comments
	if ( typeof ( social ) == 'undefined' ) return;
	var action = { action : 'social.ajax.get_objects', action_name : 'news', ids : ids };
	//retrive all facebook information
	liwe.AJAX.easy ( action, function ( resp ) 
	{
		var data =  resp [ 'list' ];
		data.iterate ( function ( _post, _id )
		{
			var _fb_like_el = 'fb_like:' + _id ;
			//FIXME il link al post di facebook non funziona
			var _fb_like_href = 'http://www.facebook.com/' + _post [ 'post_id' ].replace ( '_' , '/posts/' );
			social.facebook.like ( _fb_like_el,{ href : _fb_like_href ,  layout : "button_count", send : false,  show_faces : false }  ); 		
			 $ ( 'fb_comment:' + _id, _post [ 'comments' ] [ 'count' ]);
			 liwe.dom.add_class (  $ ( 'fb_comment:' + _id ), "fb_comment"); 
			 liwe.dom.add_class (  $ ('fb_like:' + _id ), "fb_like"); 

		});
		social.parse_all();
	} );
	
};

